WWL.accordion = function ( name ) 
{
	var acc = new WWL ( 'accordion', name );

	acc.templates = null;
	acc._load_tmpl = function ( cback )
	{
		if ( ! cback ) cback = function () {  acc._tmpl = true; acc.templates = WWL.accordion.templates };
		liwe.utils.append_js ( 'templates/accordion.js', cback );	
	};
	acc._items = [];
	acc._buttons = {};
	acc._tmpl = false;
	acc._open_tab = -1;
	//acc._load_tmpl ();
	acc._event_names = [];
	acc._id_pattern = 'acc_%(inst_name)s_%(order)s';
	
	acc.mode = "V";
	acc.tab_size = 'auto';
	acc.duration = 300;
	acc.auto_close = true;

	acc.clear = function ()
	{
		acc._items = [];
		//console.debug ( clear );
	};

	// txt - Text of item
	// content - innerhtml of accordion elements
	// cback - callback function
	// style - Item style name
	// tooltip - Tooltip text (if not defined, the 'txt' attribute is used)
	// ref_name - reference name
	// disabled - T/F for disabled item
	// info - User defined Info
	acc.add_item = function ( attrs )
	{
		attrs [ 'open' ] = false;
		acc._items.push ( attrs );
	}; 

	acc.click = function ( tab_number )
	{
		tab_number -= 1;
		var el = String.formatDict ( acc._id_pattern, { inst_name: name, order: tab_number } );
		WWL.accordion.on_click ( name, el ); 
	};
	// NOTE: setting first tab it will use WWL render cbacks and it will overwrite existing one
	// You still can pass it as a parameter to first tab function.
	acc.first_tab = function ( tab_number, ft_cback )
	{
		tab_number -= 1;
		var el = String.formatDict ( acc._id_pattern, { inst_name: name, order: tab_number } );
		acc.cbacks [ 'render' ] = function () 
			{ 
				WWL.accordion.on_click ( name, el, ft_cback ); 
			};
	};

	acc.to_string = function ()
	{
		acc.templates = WWL.accordion.templates;
		var out = new String.buffer ();
		
		out.add ( acc.templates [ 'acc-head-' + acc.mode.toLowerCase () ] );
		acc._items.iterate ( function ( v, k )
			{
				v [ 'tmp_id' ] = String.formatDict ( acc._id_pattern, { inst_name: name, order: k } );
				v [ 'inst_name' ] = acc.name;
				v [ 'cback' ] = v.get ( "cback", "''" );
				out.add ( String.formatDict ( acc.templates [ 'acc-element-' + acc.mode.toLowerCase () ], v ) );
			} );	
		out.add ( acc.templates [ 'acc-footer-' + acc.mode.toLowerCase () ] );
		return out.get ();	
	}; 

	return acc;	
};

WWL.accordion.set_mode = function ( instance_name , mode )
{
	//FIXME: not finished yet. 
	if ( mode != 'H' || mode != 'V' ) return;
	var inst = WWL.accordion.get_instance ( instance_name );
	inst.mode = mode;
};

WWL.accordion.set_duration = function ( instance_name , duration )
{
	var inst = WWL.accordion.get_instance ( instance_name );
	inst.duration = duration;
};

WWL.accordion.set_tab_size = function ( instance_name , size )
{
	var inst = WWL.accordion.get_instance ( instance_name );
	inst.tab_size = size;
};

WWL.accordion.set_first_tab = function ( instance_name, tab_number, cback )
{
	var inst = WWL.accordion.get_instance ( instance_name );
	inst.first_tab ( tab_number, cback );
};

WWL.accordion.on_click = function ( instance_name, el, cback )
{
	var inst = WWL.accordion.get_instance ( instance_name );
	var tmp_id, open_tab, elems = inst._items ;
	var tab, tab_close, sizes_close = [], sizes = [];
	sizes [ 'H' ] = [ inst.tab_size, '100%' ];
	sizes [ 'V' ] = [ '100%' , inst.tab_size ];
	sizes_close [ 'H' ] = [ '0px' , '100%' ];
	sizes_close [ 'V' ] = [ '100%', '0px' ];
	tab = sizes [ inst.mode ];
	tab_close = sizes_close [ inst.mode ];

	function _auto ()
	{
		var w = liwe.dom.get_size ( $( tmp_id ).firstChild ) [ 0 ];
		var h = liwe.dom.get_size ( $( tmp_id ).firstChild ) [ 1 ];
		sizes [ 'H' ] = [ w + 'px' , '100%' ];
		sizes [ 'V' ] = [ '100%' , h + 'px' ];
		tab = sizes [ inst.mode ];
	};	

	//console.debug ( "el = %s", el );

	elems.iterate ( function ( v, k )
			{
				tmp_id = String.formatDict ( inst._id_pattern, { inst_name: instance_name, order: k } );
				if ( ! $( tmp_id ) ) return;

				var _cback = function ()
				{
					if ( cback ) cback ( el, instance_name );
				}

				var _open = function ()
				{
					//console.debug ( 'OPEN: %s', el );
					liwe.fx.resize ( tmp_id, tab [ 0 ], tab [ 1 ], function () { v.open = true; _cback(); }, inst.duration ); 
				}

				var _close = function ()
				{
					//console.debug ( 'CLOSE: %s', el );
					liwe.fx.resize ( tmp_id, tab_close [ 0 ], tab_close [ 1 ], function () { v.open = false; }, inst.duration ); 
				}

				//open_tab = '';
				/*
				if ( el == tmp_id ) 
				{
					if ( inst.tab_size == 'auto' ) _auto (); 
					open_tab = function () 
						{ 
							if ( k == inst._open_tab )  { inst._open_tab = -1; return; }
							inst._open_tab = k;
							liwe.fx.resize ( el, tab [ 0 ], tab [ 1 ], function () { if ( cback ) cback ( el, instance_name ); }, inst.duration ); 
						};
				}
				*/
				//console.debug ( "%s [ %d ] -> %s", tmp_id, k, v.open );
				if ( inst.tab_size == 'auto' ) _auto (); 
				if ( el == tmp_id )
				{
					if ( v.open )
						_close ();
					else
						_open ();
				}
				else if ( v.open && inst.auto_close )
					_close ();
			} );
};

WWL.accordion.get_instance = function ( instance_name )
{
	return WWL.get_instance ( "accordion", instance_name );
};
